"use strict";

new Page().init();


function Page() {
    this.EXAMPLE_PANEL_ID = "j-expamle-panel"; // 提示栏的节点ID
    this.HOT_WORDS_ID = "j-hot-words"; // 热门词的容器
    this.SEARCH_PANEL_CONTAINER = "j-search-panel-container"; // 搜索输入键盘的容器ID
    this.SEARCH_RESULT_LIST_DOM = "j-search-reault"; // 搜索结果列表的容器ID
    this.RIGHT_PANEL_ID = "j-right-panel"; // 右边栏主容器ID
    this.mRoot; // 焦点框架根
    this.mKeyPanel = new KeyPanel(); // 键盘
    this.mExamplePanelDOM = document.getElementById(this.EXAMPLE_PANEL_ID); // 默认显示
    this.mSearchResultListDOM; // 搜索结果列表的容器
    this.mRightPanel = document.getElementById(this.RIGHT_PANEL_ID); // 右边栏主容器
    this.grid = null;

    this.searchKeyword = '';
    this.tip = document.getElementById('j-search-tip');
    this.noResult = document.getElementById('j-search-no-data');
    this.gridDom = document.getElementById('grid');


    this.init = function () {
        var self = this,
            focusTimer = null;
        self.mKeyPanel.setRenderListener(self.render.bind(self));
        self.mKeyPanel.setLetterChangedListener(self.letterChanged.bind(self));
        self.mKeyPanel.init(self.SEARCH_PANEL_CONTAINER);
        self.render();
        self.grid = self.mRoot.getWidgetById("grid");
        self.grid.setItemRender(function (dom, item) {
            if (item) {
                dom.querySelector('.item_image').src = item.picUrl;
                dom.querySelector('.item_name p').textContent = item.title;
                dom.querySelector('.item_num ').textContent = '更新至第2集';
                dom.querySelector('.grid-inner').style.display = 'block';
            } else {
                dom.querySelector('.grid-inner').style.display = 'none';
            }
        });

        self.grid.on('focus', function (e) {
            var index = e.target.whichData,
                curCol = Math.floor(index / self.grid.col),
                totalCol = Math.floor(self.grid.data.length / self.grid.col);
            console.log('grid focus sub-index:' + index + ',curCol:' + curCol + ',totalCol:' + totalCol);

            if (curCol === totalCol - 1) {

                self.addGridList(Test.testdata.data.result.itemList)

            }
        });


    }


    // 重新计算框架节点
    this.render = function () {
        if (this.mRoot) {
            this.mRoot.reRender();
        } else {
            this.mRoot = FocusEngine.render();
        }
        return this.mRoot;
    }

    // 输入字符发生变化的回调
    this.letterChanged = function (letters) {
        console.log("letterChanged letter=" + letters);
        //console.log("tolower letter="+letters.toLowerCase());
        var self = this;
        self.searchKeyword = letters.toLowerCase();
        if (self.searchKeyword) {

            self.updateSearchResultList(Test.testdata.data.result.record);

        } else {
            self.tip.style.display = 'block';
            self.gridDom.style.display = 'none';
            self.noResult.style.display = 'none';
        }

    }
    this.requestSearchKeyList = function () {
        //向服务端请求搜索结果
    }
    this.showNoResult = function () {
            var self = this;
            self.tip.style.display = 'none';
            self.gridDom.style.display = 'none';
            self.noResult.style.display = 'block';
        }
        // 更新搜索关键字的列表
    this.updateSearchResultList = function (itemList) {
        var self = this;
        self.tip.style.display = 'none';
        self.gridDom.style.display = 'block';
        self.gridDom.style.visibility = 'visible';
        self.noResult.style.display = 'none';
        // 创建grid
        //console.log('itemList:',itemList.length);
        this.grid.setData(itemList);
    }

    // 删除滚动关键字列表的DOM节点
    this.removeSrollListDOM = function () {
        if (this.mSearchResultListDOM) {
            var scrollListDOM = this.mSearchResultListDOM.getElementsByClassName("atv-scroll-list");
            if (scrollListDOM && scrollListDOM.length > 0) {
                this.mSearchResultListDOM.removeChild(scrollListDOM[0]);
            }
        }
    }

    //动态增加搜索结果
    this.addGridList = function (gridList) {
        var self = this;
        console.log('addGridList');
        self.grid.addData(gridList);
    }
}